package cn.txauction.web.struts.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.springframework.stereotype.Controller;

import cn.txauction.common.AppSetting;
import cn.txauction.common.Constants;
import cn.txauction.modal.TxAdmins;
import cn.txauction.modal.TxAuthority;
import cn.txauction.modal.TxMenu;
import cn.txauction.web.paging.PageView;
import cn.txauction.web.paging.PagingParam;
import cn.txauction.web.paging.PagingResult;
import cn.txauction.web.paging.PagingWebTool;
import cn.txauction.web.struts.action.base.BaseAdminAction;
import cn.txauction.web.struts.form.AdminForm;

@Controller("/tx/admin/adminManage")
public class AdminManageAction extends BaseAdminAction {

	/**
	 * 查询管理员，获取分页结果
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getAdminsByPaging(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		AdminForm empform = (AdminForm)form;
		PagingParam pagingParam = PagingWebTool.getPagingParam(empform.getCurrPage(), empform.getTotalPages());
		
		PagingResult<TxAdmins> pagingResult = adminService.getPagingAdmins(pagingParam.getFirstResult(), pagingParam.getMaxResult());
		PageView<TxAdmins> pageView = new PageView<TxAdmins>(AppSetting.pagesize,empform.getCurrPage(),AppSetting.pageBarSize); 		
		pageView.setPagingResult(pagingResult);
		
		request.setAttribute("pageView", pageView);	
		
		return mapping.findForward("tolist");
	}
	
	/**
	 * 管理员登录处理
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward adminLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		AdminForm empform = (AdminForm)form;
		TxAdmins admin = new TxAdmins(empform.getFdAdminname(), empform.getFdPassword());
		TxAdmins curAdmin = adminService.adminLogin(admin);
				
		if(curAdmin == null){
			ActionErrors messages = new ActionErrors();
			messages.add("error1", new ActionMessage("login.error"));
			saveErrors(request, messages);
			return mapping.getInputForward();
		}
		
		
		Map<Long,TxMenu> menus = new HashMap<Long,TxMenu>();
		for(TxAuthority autho: curAdmin.getTxAuthorities()){
			TxMenu menu = autho.getTxMenu();
			if(null == menu.getFdParent() || 0 == menu.getFdParent().getFdId()){
				menus.put(Long.valueOf(menu.getFdId()), menu);
			} else{
				menus.get(new Long(menu.getFdId())).getCurAdminMenus().add(menu);
			}
			
		}
		
		request.getSession().setAttribute(Constants.SessionKeys.CURRADMIN, curAdmin);
		request.getSession().setAttribute(Constants.SessionKeys.CURRADMINMENUS, menus);
		
		//更新信息		
//		map.put("fd_lastlogintime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//		systemOnLineService.updateUserNow(map, emps.get(0).getFdId().intValue());
		
		return mapping.findForward("toAdminIndex");
	}

}
